package com.sczmgk.hotline_pzh.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sczmgk.hotline_pzh.R;

/**
 * Created by Administrator on 2016/11/30 0030.
 */

public class TextItemView extends LinearLayout {

    /*文本内容*/
    private TextView etContent;

    /*是否可以获得焦点*/
    private boolean isFocusable;
    private LinearLayout mContentLayout;
    private ImageView ivArrow;
    private View viewLayout;
    private boolean isSingleLine;

    public TextItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context ,attrs);
    }

    /**
     * 初始化
     */
    private void init(Context context, AttributeSet attrs) {

        //填充布局
        View textItemView = View.inflate(context, R.layout.view_text_item, null);

        //设置布局参数
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT , LayoutParams.WRAP_CONTENT) ;
        textItemView.setLayoutParams(params);

        LinearLayout layout = (LinearLayout) textItemView.findViewById(R.id.ll_layout);
        LinearLayout layout_content = (LinearLayout) textItemView.findViewById(R.id.ll_layout_content);
//        LayoutParams paramsLayout = new LayoutParams(LayoutParams.MATCH_PARENT , LayoutParams.WRAP_CONTENT) ;
//        layout.setLayoutParams(paramsLayout);

        //文本的描述
        TextView tvDes = (TextView) textItemView.findViewById(R.id.tv_des);
        //文本的内容
        etContent = (EditText) textItemView.findViewById(R.id.et_content);
        //箭头
        ivArrow = (ImageView) textItemView.findViewById(R.id.iv_item_personal_info_arrows);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TextItemView);
        //文本的描述
        String textDes = typedArray.getString(R.styleable.TextItemView_textDes);
        //文本的hint属性
        String hint = typedArray.getString(R.styleable.TextItemView_editTextHint);
        //是否显示箭头
        boolean isShowArraw = typedArray.getBoolean(R.styleable.TextItemView_isShowArrow, false);
        int maxLines = typedArray.getInt(R.styleable.TextItemView_maxLines,1);
        //是否获得焦点
        isFocusable = typedArray.getBoolean(R.styleable.TextItemView_isEtFocusable, false);
        //是否单行
        isSingleLine = typedArray.getBoolean(R.styleable.TextItemView_isSingleLine, false);

        //设置箭头的显示
        ivArrow.setVisibility(isShowArraw ? View.VISIBLE : View.INVISIBLE);

        //设置EditText的焦点和hint
        etContent.setFocusable(isFocusable);
        etContent.setHint(hint);

        //设置文本内容的hint颜色
        if (isFocusable){
            etContent.setHintTextColor(getResources().getColor(R.color.online_hint_bg));
        }else{
            etContent.setHintTextColor(getResources().getColor(R.color.search_list_history));
        }
//        etContent.setMaxLines(maxLines);
        if (isSingleLine){
            etContent.setSingleLine();
            layout.setGravity(Gravity.CENTER_VERTICAL);
            layout_content.setGravity(Gravity.CENTER_VERTICAL);
        }

        //设置文本的描述内容
        tvDes.setText(textDes);
        typedArray.recycle();

        mContentLayout = (LinearLayout) textItemView.findViewById(R.id.ll_count);
        viewLayout = textItemView.findViewById(R.id.view_layout);

        //将填充的布局添加到当前控件中
        addView(textItemView);
    }

    /**
     * 设置文本
     */
    public void setText(String content){
        etContent.setText(content);
    }

    /**
     * 设置文本
     */
    public void setText(int content){
        etContent.setText(content);
    }

    /**
     * 获取文本
     */
    public String getText(){
        return etContent.getText().toString().trim();
    }

    /**
     * 设置条目的点击事件
     */
    public void setOnclickListener(View.OnClickListener listener){
        viewLayout.setOnClickListener(listener);
    }


}
